<template>
    <div class="app-wizard shop_star-wizardpanel  xdbdxys">
        <template class="app-wizard-content">
            <view_wizardpanel_form_WStep1 
              v-if="activeForm == 'wizardpanel_form_WStep1'" 
              :key="'wizardpanel_form_WStep1'" 
              :viewState='wizardState'
              :context="context"
              :viewparams="viewparams"
              updateAction="CompleteBasicScore"
              removeAction=""
              loadAction="Get"
              @save="wizardpanel_form_WStep1_save" 
              @load="wizardpanel_form_WStep1_load" 
              name='wizardpanel_form_WStep1' 
              ref='wizardpanel_form_WStep1'>
            </view_wizardpanel_form_WStep1>
            <view_wizardpanel_form_WStep2 
              v-if="activeForm == 'wizardpanel_form_WStep2'" 
              :key="'wizardpanel_form_WStep2'" 
              :viewState='wizardState'
              :context="context"
              :viewparams="viewparams"
              updateAction="CompleteFeatureScore"
              removeAction=""
              loadAction="Get"
              @save="wizardpanel_form_WStep2_save" 
              @load="wizardpanel_form_WStep2_load" 
              name='wizardpanel_form_WStep2' 
              ref='wizardpanel_form_WStep2'>
            </view_wizardpanel_form_WStep2>
            <view_wizardpanel_form_WStep3 
              v-if="activeForm == 'wizardpanel_form_WStep3'" 
              :key="'wizardpanel_form_WStep3'" 
              :viewState='wizardState'
              :context="context"
              :viewparams="viewparams"
              updateAction="CompEvaluate"
              removeAction=""
              loadAction="Get"
              @save="wizardpanel_form_WStep3_save" 
              @load="wizardpanel_form_WStep3_load" 
              name='wizardpanel_form_WStep3' 
              ref='wizardpanel_form_WStep3'>
            </view_wizardpanel_form_WStep3>
        </template>
        <ion-footer class="app-wizard-footer">
            <app-mob-button buttontype="PREV" v-show="!isHidden('PREV')" type="primary" :text="$t('app.button.previousStep')" @click="onClickPrev()" />
            <app-mob-button buttontype="NEXT" v-show="!isHidden('NEXT')"  type="primary" :text="$t('app.button.nextStep')" @click="onClickNext()" />
            <app-mob-button buttontype="FINISH" v-show="!isHidden('FINISH')" type="primary" :text="$t('app.button.finish')" @click="onClickFinish()" />
        </ion-footer>
    </div>
</template>
<script lang='ts'>
import { Vue, Component, Prop, Provide, Emit, Watch, Model } from 'vue-property-decorator';
import { CreateElement } from 'vue';
import { Subject, Subscription } from 'rxjs';
import { ControlInterface } from '@/interface/control';
import GlobalUiService from '@/global-ui-service/global-ui-service';
import Shop_starEntityService from '@/app-core/service/shop-star/shop-star-service';
import EvaluateService from '@/app-core/ctrl-service/shop-star/evaluate-wizardpanel-service';
import AppCenterService from "@/ibiz-core/app-service/app/app-center-service";

import Shop_starUIService from '@/ui-service/shop-star/shop-star-ui-action';



@Component({
    components: { }
})
export default class EvaluateBase extends Vue implements ControlInterface {

    /**
     * 名称
     *
     * @type {string}
     * @memberof Evaluate
     */
    @Prop() protected name?: string;

    /**
     * 视图名称
     *
     * @type {string}
     * @memberof Evaluate
     */
    @Prop() protected viewName!: string;


    /**
     * 视图通讯对象
     *
     * @type {Subject<ViewState>}
     * @memberof Evaluate
     */
    @Prop() protected viewState!: Subject<ViewState>;

    /**
     * 应用上下文
     *
     * @type {*}
     * @memberof Evaluate
     */
    @Prop({ default: {} }) protected context?: any;

    /**
     * 视图参数
     *
     * @type {*}
     * @memberof Evaluate
     */
    @Prop({ default: {} }) protected viewparams?: any;

    /**
     * 视图状态事件
     *
     * @protected
     * @type {(Subscription | undefined)}
     * @memberof Evaluate
     */
    protected viewStateEvent: Subscription | undefined;

    /**
     * 获取部件类型
     *
     * @returns {string}
     * @memberof Evaluate
     */
    protected getControlType(): string {
        return 'WIZARDPANEL'
    }

    /**
     * 全局 ui 服务
     *
     * @type {GlobalUiService}
     * @memberof Evaluate
     */
    protected globaluiservice: GlobalUiService = new GlobalUiService();


    /**
     * 转化数据
     *
     * @param {any} args
     * @memberof  EvaluateBase
     */
    public transformData(args: any) {
        let _this: any = this;
        if(_this.service && _this.service.handleRequestData instanceof Function && _this.service.handleRequestData('transform',_this.context,args)){
            return _this.service.handleRequestData('transform',_this.context,args);
        }
    }

    /**
     * 建构部件服务对象
     *
     * @type {EvaluateService}
     * @memberof Evaluate
     */
    protected service: EvaluateService = new EvaluateService({$store:this.$store});

    /**
     * 实体服务对象
     *
     * @type {Shop_starService}
     * @memberof Evaluate
     */
    protected appEntityService: Shop_starEntityService = new Shop_starEntityService();

    /**
     * 界面UI服务对象
     *
     * @type {Shop_starUIService}
     * @memberof EvaluateBase
     */  
    public deUIService:Shop_starUIService = new Shop_starUIService(this.$store);

    /**
     * wizardpanel_form_WStep3 部件 save 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof Evaluate
     */
    protected wizardpanel_form_WStep3_save($event: any, $event2?: any) {
        this.wizardpanel_formsave($event, 'wizardpanel_form_WStep3', $event2);
    }

    /**
     * wizardpanel_form_WStep3 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof Evaluate
     */
    protected wizardpanel_form_WStep3_load($event: any, $event2?: any) {
        this.wizardpanel_formload($event, 'wizardpanel_form_WStep3', $event2);
    }

    /**
     * wizardpanel_form_WStep2 部件 save 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof Evaluate
     */
    protected wizardpanel_form_WStep2_save($event: any, $event2?: any) {
        this.wizardpanel_formsave($event, 'wizardpanel_form_WStep2', $event2);
    }

    /**
     * wizardpanel_form_WStep2 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof Evaluate
     */
    protected wizardpanel_form_WStep2_load($event: any, $event2?: any) {
        this.wizardpanel_formload($event, 'wizardpanel_form_WStep2', $event2);
    }

    /**
     * wizardpanel_form_WStep1 部件 save 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof Evaluate
     */
    protected wizardpanel_form_WStep1_save($event: any, $event2?: any) {
        this.wizardpanel_formsave($event, 'wizardpanel_form_WStep1', $event2);
    }

    /**
     * wizardpanel_form_WStep1 部件 load 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof Evaluate
     */
    protected wizardpanel_form_WStep1_load($event: any, $event2?: any) {
        this.wizardpanel_formload($event, 'wizardpanel_form_WStep1', $event2);
    }
    

    /**
     * 关闭视图
     *
     * @param {any[]} args
     * @memberof Evaluate
     */
    protected closeView(args: any[]): void {
        let _this: any = this;
        _this.$emit('closeview', args);
    }
    
    /**
     * 部件行为--init
     *
     * @type {string}
     * @memberof Evaluate
     */
    @Prop() public initAction!: string;
    
    /**
     * 部件行为--finish
     *
     * @type {string}
     * @memberof Evaluate
     */
    @Prop() public finishAction!: string;

    /**
     * 显示处理提示
     *
     * @type {boolean}
     * @memberof Evaluate
     */
    @Prop({ default: true }) public showBusyIndicator?: boolean;

    /**
      * 获取多项数据
      *
      * @returns {any[]}
      * @memberof Evaluate
      */
    public getDatas(): any[] {
        return [this.formParam];
    }

    /**
      * 获取单项数据
      *
      * @returns {*}
      * @memberof Evaluate
      */
    public getData(): any {
        return this.formParam;
    }

    /**
     * 视图状态订阅对象
     *
     * @public
     * @type {Subject<{action: string, data: any}>}
     * @memberof Evaluate
     */
    public wizardState: Subject<ViewState> = new Subject();

    /**
     * 当前激活表单
     *
     * @type {string}
     * @memberof Evaluate
     */
    public activeForm: string = 'wizardpanel_form_WStep1';

    /**
     * 向导表单参数
     *
     * @type {*}
     * @memberof Evaluate
     */
    public formParam: any = {};

    /**
     * 执行过的表单
     *
     * @public
     * @type {Array<string>}
     * @memberof Evaluate
     */
    public historyForms: Array<string> = [];

    /**
     * 步骤行为集合
     *
     * @type {*}
     * @memberof Evaluate
     */
    public stepActions: any = {};

    /**
     * 向导表单集合
     *
     * @type {Array<any>}
     * @memberof Evaluate
     */
    public wizardForms: Array<any> = [];

    /**
     * 当前状态
     *
     * @memberof Evaluate
     */
    public curState = '';

    /**
     * Vue声明周期(处理组件的输入属性)
     *
     * @memberof Evaluate
     */
    public created(): void {
        this.regFormActions();
        if(this.activeForm) {
            this.historyForms.push(this.activeForm);
        }
        if (this.viewState) {
            this.viewStateEvent = this.viewState.subscribe(({ tag, action, data }) => {
                if (Object.is(tag, this.name)) {
                    if (Object.is('load', action)) {
                        this.doInit(data);
                    }
                }
            });
        }
    }

    /**
     * vue 生命周期
     *
     * @memberof Evaluate
     */
    public destroyed() {
        if (this.viewStateEvent) {
            this.viewStateEvent.unsubscribe();
        }
    }

    /**
     * 注册表单步骤行为
     *
     * @memberof Evaluate
     */
    public regFormActions() {
        this.regFormAction('wizardpanel_form_WStep1', ['NEXT']);
        this.regFormAction('wizardpanel_form_WStep2', ['PREV','NEXT']);
        this.regFormAction('wizardpanel_form_WStep3', ['PREV','FINISH']);
    }

    /**
     * 注册表单步骤行为
     *
     * @memberof Evaluate
     */
    public regFormAction(name: string, actions: Array<string>) {
        this.stepActions[name] = actions;
        this.wizardForms.push(name);
    }

    /**
     * 初始化行为
     *
     * @memberof Evaluate
     */
    public doInit(opt: any = {}) {
        const arg: any = { ...opt };
        Object.assign(arg,{viewparams:this.viewparams});
        const post: Promise<any> = this.service.init(this.initAction, JSON.parse(JSON.stringify(this.context)), arg, this.showBusyIndicator);
        post.then((response: any) => {
            if (response && response.status === 200) {
                this.formParam = response.data;
                if(response.data.shop_star){
                    Object.assign(this.context,{shop_star:response.data.shop_star})
                }
                this.formLoad();
            }
        }).catch((response: any) => {
            if (response && response.status === 401) {
                return;
            }
        });
    }

    /**
     * 表单加载
     *
     * @memberof Evaluate
     */
    public formLoad() {
        if(this.activeForm) {
            this.wizardState.next({ tag: this.activeForm, action: 'load', data: this.formParam });
        }
    }

    /**
     * 完成行为
     *
     * @memberof Evaluate
     */
    public doFinish() {
        let arg: any = {};
        Object.assign(arg, this.formParam);
        Object.assign(arg,{viewparams:this.viewparams});
        const post: Promise<any> = this.service.finish(this.finishAction, JSON.parse(JSON.stringify(this.context)), arg, this.showBusyIndicator);
        post.then((response: any) => {
            if (response && response.status === 200) {
                const data = response.data;
                this.$emit("finish", data);
                this.$emit("closeview", data);
            }
        }).catch((response: any) => {
            if (response && response.status === 401) {
                return;
            }
            this.$notice.warning( response.info );
        });
    }

    /**
     * 向导表单加载完成
     *
     * @param {*} args
     * @param {string} name
     * @memberof Evaluate
     */
    public wizardpanel_formload(args: any, name: string, $event2?: any) {
        if(args) {
            Object.assign(this.formParam, args);
        }
    }

    /**
     * 向导表单保存完成
     *
     * @param {*} args
     * @param {string} name
     * @memberof Evaluate
     */
    public wizardpanel_formsave(args: any, name: string, $event2?: any) {
        Object.assign(this.formParam, args);
        if(Object.is(this.curState, 'NEXT')) {
            this.historyForms.push(name);
            if (this.getNextForm()) {
                this.activeForm = this.getNextForm();
                setTimeout(() => {
                    this.formLoad();
                }, 1);
            } else {
                this.doFinish();
            }
        }else if(Object.is(this.curState, 'FINISH')) {
            this.doFinish();
        }
    }

    /**
     * 获取下一步向导表单
     *
     * @memberof Evaluate
     */
    public getNextForm() {
        let index = this.wizardForms.indexOf(this.activeForm);
        if(index >= 0) {
            if(this.wizardForms[index + 1]) {
                return this.wizardForms[index + 1];
            }
        }
        return undefined;
    }


    /**
     * 上一步
     *
     * @memberof Evaluate
     */
    public onClickPrev() {
        const length = this.historyForms.length;
        if(length > 1) {
            this.curState = 'PREV';
            this.activeForm = this.historyForms[length - 1];
            setTimeout(() => {
                this.formLoad();
            }, 1);
            this.historyForms.splice(length - 1, 1);
        }
    }

    /**
     * 下一步
     *
     * @memberof Evaluate
     */
    public async onClickNext() {
        if(this.activeForm) {
            if(this.$refs && this.$refs[this.activeForm]){
                let form: any = this.$refs[this.activeForm];
                if(await form.validAll()) {
                    this.curState = 'NEXT';
                    this.wizardState.next({ tag: this.activeForm, action: 'save', data: this.formParam });
                } else {
                    this.$notice.warning( '值规则校验异常' );
                }
            }
        }
    }

    /**
     * 完成
     *
     * @memberof Evaluate
     */
    public async onClickFinish() {
        if(this.activeForm) {
            if(this.$refs && this.$refs[this.activeForm]){
                let form: any = this.$refs[this.activeForm];
                if(await form.validAll()) {
                    this.curState = 'FINISH';
                    this.wizardState.next({ tag: this.activeForm, action: 'save', data: this.formParam });
                } else {
                     this.$notice.warning( '值规则校验异常' );
                }
            }
        }
    }

    /**
     * 是否禁用
     *
     * @param {string} type
     * @memberof Evaluate
     */
    public isHidden(type: string) {
        const actions: Array<string> = this.stepActions[this.activeForm]
        if(actions && actions.indexOf(type) < 0) {
            return true;
        }
        return false;
    }
    

}
</script>

<style lang='less'>
@import './evaluate-wizardpanel.less';
</style>